package com.ewzj.bigdata.app.utils;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.mvp.model.api.Api;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.utils.DeviceUtils;

import java.io.File;
import java.lang.reflect.Field;
import java.text.DecimalFormat;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Author:zhangjing
 * Time:2018/1/26  下午2:21
 * Name: this is Apputils
 * Email:zhangjingworkmol@163.com
 */

public class Apputils {
    /**
     *
     * @param json
     * @return传入json对象转化为RequestBody对象，retrofit的post  raw
     */
    public static RequestBody json2Body(String json){
        RequestBody requestBody=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json);
        return requestBody;
    }

    /**
     * 文件转化为requestbody
     * @param pFile
     * @return
     */
    public static RequestBody toRequestBodyOfImage(File pFile){

        RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), pFile);
        return fileBody;
    }

    /**
     *
     * @param context
     * @param tabs
     * @param leftDip
     * @param rightDip
     */
    public static void setIndicator(Context context, TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout ll_tab = null;
        try {
            ll_tab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) (DeviceUtils.getDisplayMetrics(context).density * leftDip);
        int right = (int) (DeviceUtils.getDisplayMetrics(context).density * rightDip);

        for (int i = 0; i < ll_tab.getChildCount(); i++) {
            View child = ll_tab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }
/**
 * 转换文件大小
 *
 * @param fileS
 * @return
 */
    public static String FormetFileSize(long fileS) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + "B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + "KB";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + "MB";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + "G";
        }
        return fileSizeString;
    }

    public static String getFromatUrl(String url){
        if (!url.startsWith("http")){
            return Api.APP_DOMAIN+url;
        }else {
            return url;
        }
    }

    /**
     * app状态是否是已经登录的状态
     * @param context
     * @return
     */
    public static Boolean isLogin(Context context){
        return !TextUtils.isEmpty(DataHelper.getStringSF(context,AppConstants.APP_TOKEN));
    }
    /**
     *
     * 文件上传
     */
    public static void uploadFile(){

    }

    /**
     * oss上传后的地址
     */
    public static String getOssFileUrl(String object){
        return AppConstants.OSS_PRE+object;
    }

}
